package com.lovecaa.delay;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * 延迟消息生产者
 * 创建时间： 2023年10月22日 18:06:00
 *
 * @author Mr.Tao
 * @since 1.0.0
 */
public class DelayMessageProducer {
    public static void main(String[] args) {
        try {
            DefaultMQProducer producer = new DefaultMQProducer("SyncProducer");
            producer.setNamesrvAddr("192.168.192.110:9876");
            producer.start();
            for (int i = 0; i < 6; i++) {
                String topic = "Test1";
                String tag = "TagA";
                byte[] msg = ("SyncProducer_" + i).getBytes(StandardCharsets.UTF_8);
                Message message = new Message(topic, tag, msg);
                // 有18个级别1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
                message.setDelayTimeLevel(2);
                // 在5.0以后新增的API，可以直接设置延迟毫秒数，最大3天
                // message.setDelayTimeMs(5000L);
                SendResult result = producer.send(message);
                System.out.println("消息" + i + "发送成功:" + result);
            }
            producer.shutdown();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}
